//初始化信息
$(function(){
	getSession();//初始化session对象
})



//验证登录
function ggxygetSession(url){
	
	$.ajax({
		  type: "post",
		  url: "../qtuser/getSession",
		  dataType: "json",
		  success: function(data){
			  if(data.zt==0){
				 location.href=url;
			  }else{
				 alert('请先登录！');
				 MM_showHideLayers('loginbg','','show');
			  }
		  }
	  })
}
//首页登录框
function MM_showHideLayers() { //v9.0
	  $('#img').val("");
	  $('#img2').val("");
	  var i,p,v,obj,args=MM_showHideLayers.arguments;
	  for (i=0; i<(args.length-2); i+=3) 
	  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
	    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
	    obj.visibility=v; }
}

//首页登录
function login() { 
	var username = $('#img').val();
	var password = $('#img2').val();
	username = username.replace(/\s/g, "");
	password = password.replace(/\s/g, "");
	if(username==""){alert("用户名不能为空！"); return;}
	if(password==""){alert("密码不能为空！"); return;}
	$.ajax({
		  type: "post",
		  url: "../qtuser/queryUser",
		  data: {
			  'username':encode64(username),//用户名
			  'password':encode64(password)//密码
		  },
		  dataType: "json",
		  success: function(data){
				if(data.msg=="登陆成功！"){
					MM_showHideLayers('loginbg','','hide');
					$("#toptl").empty();
					var htmlStr=' <li class="ml15 mr10" ><i class="i i_pc"></i><a id="loginMsg" href="javascript:void(0)"></a></li>'
				       +' <li class="spacer"></li> <li class="mr10"><i class="i i_res"></i><a id="loginMsg1" href="javascript:void(0)">注销2</a></li> '
				       +'<li class="spacer"></li><li><i class="i i_colle"></i><a href="javascript:sc(window.location,document.title)">收藏</a></li>';
					$("#toptl").append(htmlStr);
					$("#loginMsg").text(username);
				    $("#loginMsg").attr("onclick","");
				    $("#loginMsg1").text("注销");
					$("#loginMsg1").attr("onclick","zx();");
				}else{
					MM_showHideLayers('loginbg','','hide')
					$('#img').val("");
					$('#img2').val("");
				}
		  }
	  })
}
//初始化session中的用户
function getSession() {
	$.ajax({
		  type: "post",
		  url: "../qtuser/getSession",
		  dataType: "json",
		  success: function(data){
			  if(data.zt==0){//登录中
				    $("#toptl").empty();
				    var htmlStr=' <li class="ml15 mr10" ><i class="i i_pc"></i><a id="loginMsg" href="javascript:void(0)"  ></a></li>'
				       +' <li class="spacer"></li> <li class="mr10"><i class="i i_res"></i><a id="loginMsg1" href="javascript:void(0)">注销2</a></li> '
				       +'<li class="spacer"></li><li><i class="i i_colle"></i><a href="javascript:sc(window.location,document.title)">收藏</a></li>';
					
					$("#toptl").append(htmlStr);
				    $("#loginMsg").text(data.username);
				    $("#loginMsg").attr("onclick","");
					$("#loginMsg1").text("注销");
					$("#loginMsg1").attr("onclick","zx();");
					$('#username').val(username);
					$('#password').val(password);
					$('#img').val("");
					$('#img2').val("");
			  }else{//未登录
				  $("#toptl").empty();
				  var htmlStr=' <li class="ml15 mr10" ><i class="i i_pc"></i><a id="loginMsg" href="javascript:MM_showHideLayers('+"'loginbg'"+','+"''"+','+"'show'"+');"  >登录</a></li>'
			       +' <li class="spacer"></li> <li class="mr10"><i class="i i_res"></i><a id="loginMsg1" href="page_register.html?parent=90">注册</a></li> '
			       +'<li class="spacer"></li><li><i class="i i_colle"></i><a href="javascript:sc(window.location,document.title)">收藏</a></li>';
				  $("#toptl").append(htmlStr);
			  }
		  }
	  })
}
//注销方法
function zx() {
	$.ajax({
		  type: "post",
		  url: "../qtuser/removeSession",
		  dataType: "json",
		  success: function(data){
	      $("#toptl").empty();
	      var htmlStr=' <li class="ml15 mr10" ><i class="i i_pc"></i><a id="loginMsg" href="javascript:MM_showHideLayers('+"'loginbg'"+','+"''"+','+"'show'"+');"  >登录</a></li>'
	       +' <li class="spacer"></li> <li class="mr10"><i class="i i_res"></i><a id="loginMsg1" href="page_register.html?parent=90">注册</a></li> '
	       +'<li class="spacer"></li><li><i class="i i_colle"></i><a href="javascript:sc(window.location,document.title)">收藏</a></li>';
		  $("#toptl").append(htmlStr);
		 }
	  })
}
//收藏页面
function sc(sURL, sTitle){
	   sURL = encodeURI(sURL); 
       try{   
           window.external.addFavorite(sURL, sTitle);   
       }catch(e) {   
           try{   
               window.sidebar.addPanel(sTitle, sURL, "");   
           }catch (e) {   
               alert("加入收藏失败，请使用Ctrl+D进行添加,或手动在浏览器里进行设置.");
           }   
       }
}


//base64加密开始
var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv"
                + "wxyz0123456789+/" + "=";

function encode64(input) {
        var output = "";
        var chr1, chr2, chr3 = "";
        var enc1, enc2, enc3, enc4 = "";
        var i = 0;
        do {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;
                if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                        enc4 = 64;
                }
                output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
                                + keyStr.charAt(enc3) + keyStr.charAt(enc4);
                chr1 = chr2 = chr3 = "";
                enc1 = enc2 = enc3 = enc4 = "";
        } while (i < input.length);

        return output;
}
// base64加密结束